package bb.lanxing.lib.widget;

import android.content.Context;
import android.util.AttributeSet;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class SectionRecyclerScrollTabStrip extends BaseScrollTabStrip {
    private final RecyclerView.AdapterDataObserver adapterDataObserver;
    private int currentSection;
    private RecyclerView recyclerView;
    private SectionedRecyclerViewAdapter<? extends RecyclerView.ViewHolder, ? extends RecyclerView.ViewHolder, ? extends RecyclerView.ViewHolder> recyclerViewAdapter;
    private boolean registerObserver;
    private boolean reverse;

    public SectionRecyclerScrollTabStrip(Context context) {
        super(context);
        this.reverse = true;
        this.registerObserver = false;
        this.adapterDataObserver = new RecyclerView.AdapterDataObserver() {
            private final Runnable updateRunnable = new Runnable() {
                @Override
                public void run() {
                    int sectionCount = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionCount();
                    SectionRecyclerScrollTabStrip.this.titles = new String[sectionCount];
                    for (int i = 0; i < sectionCount; i++) {
                        SectionRecyclerScrollTabStrip.this.titles[i] = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionTitle(SectionRecyclerScrollTabStrip.this.reverse ? (sectionCount - 1) - i : i).toString();
                    }
                    SectionRecyclerScrollTabStrip.this.currentPosition = SectionRecyclerScrollTabStrip.this.currentPosition >= sectionCount ? sectionCount - 1 : SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.currentSection = SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.notifyAllTabs();
                    SectionRecyclerScrollTabStrip.this.setUpCurrentTab(SectionRecyclerScrollTabStrip.this.recyclerView);
                }
            };

            @Override
            public void onItemRangeRemoved(int i, int i2) {
                super.onItemRangeRemoved(i, i2);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onItemRangeInserted(int i, int i2) {
                super.onItemRangeInserted(i, i2);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onChanged() {
                super.onChanged();
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }
        };
    }

    public SectionRecyclerScrollTabStrip(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.reverse = true;
        this.registerObserver = false;
        this.adapterDataObserver = new RecyclerView.AdapterDataObserver() {
            private final Runnable updateRunnable = new Runnable() {
                @Override
                public void run() {
                    int sectionCount = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionCount();
                    SectionRecyclerScrollTabStrip.this.titles = new String[sectionCount];
                    for (int i = 0; i < sectionCount; i++) {
                        SectionRecyclerScrollTabStrip.this.titles[i] = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionTitle(SectionRecyclerScrollTabStrip.this.reverse ? (sectionCount - 1) - i : i).toString();
                    }
                    SectionRecyclerScrollTabStrip.this.currentPosition = SectionRecyclerScrollTabStrip.this.currentPosition >= sectionCount ? sectionCount - 1 : SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.currentSection = SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.notifyAllTabs();
                    SectionRecyclerScrollTabStrip.this.setUpCurrentTab(SectionRecyclerScrollTabStrip.this.recyclerView);
                }
            };

            @Override
            public void onItemRangeRemoved(int i, int i2) {
                super.onItemRangeRemoved(i, i2);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onItemRangeInserted(int i, int i2) {
                super.onItemRangeInserted(i, i2);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onChanged() {
                super.onChanged();
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }
        };
    }

    public SectionRecyclerScrollTabStrip(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.reverse = true;
        this.registerObserver = false;
        this.adapterDataObserver = new RecyclerView.AdapterDataObserver() {
            private final Runnable updateRunnable = new Runnable() {
                @Override
                public void run() {
                    int sectionCount = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionCount();
                    SectionRecyclerScrollTabStrip.this.titles = new String[sectionCount];
                    for (int i2 = 0; i2 < sectionCount; i2++) {
                        SectionRecyclerScrollTabStrip.this.titles[i2] = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionTitle(SectionRecyclerScrollTabStrip.this.reverse ? (sectionCount - 1) - i2 : i2).toString();
                    }
                    SectionRecyclerScrollTabStrip.this.currentPosition = SectionRecyclerScrollTabStrip.this.currentPosition >= sectionCount ? sectionCount - 1 : SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.currentSection = SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.notifyAllTabs();
                    SectionRecyclerScrollTabStrip.this.setUpCurrentTab(SectionRecyclerScrollTabStrip.this.recyclerView);
                }
            };

            @Override
            public void onItemRangeRemoved(int i2, int i22) {
                super.onItemRangeRemoved(i2, i22);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onItemRangeInserted(int i2, int i22) {
                super.onItemRangeInserted(i2, i22);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onChanged() {
                super.onChanged();
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }
        };
    }

    public SectionRecyclerScrollTabStrip(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
        this.reverse = true;
        this.registerObserver = false;
        this.adapterDataObserver = new RecyclerView.AdapterDataObserver() {
            private final Runnable updateRunnable = new Runnable() {
                @Override
                public void run() {
                    int sectionCount = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionCount();
                    SectionRecyclerScrollTabStrip.this.titles = new String[sectionCount];
                    for (int i22 = 0; i22 < sectionCount; i22++) {
                        SectionRecyclerScrollTabStrip.this.titles[i22] = SectionRecyclerScrollTabStrip.this.recyclerViewAdapter.getSectionTitle(SectionRecyclerScrollTabStrip.this.reverse ? (sectionCount - 1) - i22 : i22).toString();
                    }
                    SectionRecyclerScrollTabStrip.this.currentPosition = SectionRecyclerScrollTabStrip.this.currentPosition >= sectionCount ? sectionCount - 1 : SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.currentSection = SectionRecyclerScrollTabStrip.this.currentPosition;
                    SectionRecyclerScrollTabStrip.this.notifyAllTabs();
                    SectionRecyclerScrollTabStrip.this.setUpCurrentTab(SectionRecyclerScrollTabStrip.this.recyclerView);
                }
            };

            @Override
            public void onItemRangeRemoved(int i22, int i222) {
                super.onItemRangeRemoved(i22, i222);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onItemRangeInserted(int i22, int i222) {
                super.onItemRangeInserted(i22, i222);
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }

            @Override
            public void onChanged() {
                super.onChanged();
                if (SectionRecyclerScrollTabStrip.this.recyclerView.getHandler() != null) {
                    SectionRecyclerScrollTabStrip.this.recyclerView.getHandler().removeCallbacks(this.updateRunnable);
                }
                SectionRecyclerScrollTabStrip.this.recyclerView.postDelayed(this.updateRunnable, 100L);
            }
        };
    }

    @Override
    protected void onTabClick(int i) {
        if (this.reverse) {
            i = (this.recyclerViewAdapter.getSectionCount() - i) - 1;
        }
        ((LinearLayoutManager) this.recyclerView.getLayoutManager()).scrollToPositionWithOffset(this.recyclerViewAdapter.getItemPosition(i), 0);
        this.currentSection = i;
    }

    public void setUpRecyclerView(RecyclerView recyclerView, SectionedRecyclerViewAdapter<? extends RecyclerView.ViewHolder, ? extends RecyclerView.ViewHolder, ? extends RecyclerView.ViewHolder> sectionedRecyclerViewAdapter, boolean reverse) {
        this.recyclerView = recyclerView;
        this.reverse = reverse;
        this.recyclerViewAdapter = sectionedRecyclerViewAdapter;
        int sectionCount = sectionedRecyclerViewAdapter.getSectionCount();
        this.titles = new String[sectionCount];
        int i = 0;
        for (int i2 = 0; i2 < sectionCount; i2++) {
            this.titles[i2] = sectionedRecyclerViewAdapter.getSectionTitle(reverse ? (sectionCount - 1) - i2 : i2).toString();
        }
        if (reverse) {
            i = sectionCount - 1;
        }
        this.currentSection = i;
        this.currentPosition = i;
        notifyAllTabs();
        post(new Runnable() {
            @Override
            public void run() {
                SectionRecyclerScrollTabStrip sectionRecyclerScrollTabStrip = SectionRecyclerScrollTabStrip.this;
                sectionRecyclerScrollTabStrip.scrollToChild(sectionRecyclerScrollTabStrip.currentPosition, 0);
            }
        });
        if (!this.registerObserver) {
            recyclerView.addOnScrollListener(new OnSectionChangedListener());
            this.recyclerViewAdapter.registerAdapterDataObserver(this.adapterDataObserver);
            this.registerObserver = true;
        }
    }


    public void setUpCurrentTab(RecyclerView recyclerView) {
        int sectionForPosition;
        int findFirstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
        if (findFirstVisibleItemPosition >= 0 && (sectionForPosition = this.recyclerViewAdapter.getSectionForPosition(findFirstVisibleItemPosition)) >= 0 && this.currentSection != sectionForPosition) {
            final int sectionCount = this.reverse ? (this.recyclerViewAdapter.getSectionCount() - sectionForPosition) - 1 : sectionForPosition;
            int sectionCount2 = this.reverse ? (this.recyclerViewAdapter.getSectionCount() - this.currentSection) - 1 : this.currentSection;
            post(new Runnable() {
                @Override
                public void run() {
                    SectionRecyclerScrollTabStrip.this.scrollToChild(sectionCount, 0);
                }
            });
            switchTabTextColor(sectionCount2, sectionCount);
            invalidate();
            this.currentSection = sectionForPosition;
        }
    }

    public class OnSectionChangedListener extends RecyclerView.OnScrollListener {
        OnSectionChangedListener() {
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int i, int i2) {
            super.onScrolled(recyclerView, i, i2);
            SectionRecyclerScrollTabStrip.this.setUpCurrentTab(recyclerView);
        }
    }
}
